package oriol.forns.gutierrez;

import java.util.ArrayList;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

public class Pilota {

	public float x;
	public float y;
	public float vx;
	public float vy;
	public float tx;
	public float ty;
	public float px;
	public float py;
	public int fire;
	public boolean iniciat;
	private int racha;
	private ArrayList<Bitmap> img;
	BitmapFactory.Options options;
	Resources resources;
	
	
	public Pilota(float by, float bx, Resources res){
		img = new ArrayList<Bitmap>();
		resources = res;
		iniciat = false;
		fire = 0;
		options = new BitmapFactory.Options();
		options.inPreferredConfig = Bitmap.Config.ARGB_8888;
		tx = ty = 20;
		y = by - ty;
		vx = 0;
		vy = 0;
		py = y;
		racha = 1;
		Bitmap aux = BitmapFactory.decodeResource(resources, R.drawable.esfera, options);
		img.add(Bitmap.createScaledBitmap(aux, (int)tx, (int)ty, false));
		aux = BitmapFactory.decodeResource(resources, R.drawable.fuego, options);
		img.add(Bitmap.createScaledBitmap(aux, (int)tx, (int)ty, false));
	}

	public void actualitza() {
		if (fire != 0) --fire;
		x +=vx;
		y +=vy;
	}

	public void barra(float bx, float btx){
		x = bx + btx/2 - (tx/2);
		px = x;
	}
	
	public void nova_pilota(float by){
		y = by - ty;
		py = y;
		vx = 0;
		vy = 0;
		racha = 1;
		iniciat = false;
		fire = 0;
	}
	
	public void inicia(){
		vx = -10;
		vy = -10;
		iniciat = true;
	}
	
	public void draw(Canvas canvas) {
		if (fire != 0) canvas.drawBitmap(img.get(1), x, y, null);
		else canvas.drawBitmap(img.get(0), x, y, null);
	}
	
	public boolean destrueix(float bx, float by, float btx, float bty){
		if(x+tx>bx && x<bx+btx && y+ty>by && y<by+bty) {
			if (fire == 0) {
				if(y<by)
					if(vy>0) vy = -vy;
				//sota
				if(y+ty>by+bty)
					if(vy<0) vy = -vy;
				//esquerra
				if(x<bx)
					if(vx>0) vx = -vx;
				//dreta
				if(x+tx>bx+btx)
					if(vx<0) vx = -vx;
			}
			GV.marcador.punts += racha;
			racha++;
			return true;
		}
		return false;
	}
	
	public void colisions(Base base, Powerups power, Shoots shoot) {
		if(y>GV.Screen.metrics.heightPixels+ty) {
			GV.marcador.vides--;
			nova_pilota(base.plataforma.y);
			base.shoot(false);
			base.nou();
			shoot.clean();
			power.clean();
		}
		else if(x>GV.Screen.metrics.widthPixels-tx) {
			vx = -vx;
			x = GV.Screen.metrics.widthPixels-tx;
			py = y;
		}
		else if (y < 0) {
			vy = -vy;
			y = 0;
			px = x;
		}
		else if (x < 0) {
			vx = -vx;
			x = 0;
			py = y;
		}
		else if(x+tx>base.plataforma.x && x<base.plataforma.x+base.plataforma.tx && y+ty>base.plataforma.y && y<base.plataforma.y+base.plataforma.ty) {
			//colisio 4
			//sobre
			if(y<base.plataforma.y)
				if(vy>0) vy = -vy;
			//sota
			if(y+ty>base.plataforma.y+base.plataforma.ty)
				if(vy<0) vy = -vy;
			//esquerra
			if(x<base.plataforma.x)
				if(vx>0) vx = -vx;
			//dreta
			if(x+tx>base.plataforma.x+base.plataforma.tx)
				if(vx<0) vx = -vx;
			racha = 1;
		}
		else {
			px = x;
			py = y;
		}
	}

	public void fire() {
		fire = 100;
	}

	public void vel(int i) {
		if (vy > 0) {
			vy += i;
			if (vy > 16) vy = 16;
			else if (vy < 6) vy = 6;
		}
		else {
			vy -=i;
			if (vy < -16) vy = -16;
			else if (vy > -6) vy = -6;
		}
		if (vx > 0){
			vx += i;
			if (vx > 16) vx = 16;
			else if (vx < 6) vx = 6;
		}
		else{
			vx -= i;
			if (vx < -16) vx = -16;
			else if (vx > -6) vx = -6;
		}
	}
}
